#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <qqthostaddressmanager.h>
#include <qqtlongdataserialization.h>

MainWindow::MainWindow ( QWidget* parent ) :
    QMainWindow ( parent ),
    ui ( new Ui::MainWindow )
{
    ui->setupUi ( this );

    bs0 = new QQtBeServerManager ( this );

    bs0->setListeningPort ( 6000 );
    QQtHostAddressManager hm0;
    QString ip = hm0.getHostAddress();
    bs0->setTellClient ( ip, 6003 );

    c0 = new QQtUdpClient ( this );
    p0 = new QQtLongDataProtocol ( this );
    c0->installProtocol ( p0 );


    c0->bind ( 6003, QQtUdpServer::DefaultForPlatform );
    connect ( p0, SIGNAL ( readyRead ( const QQtLongDataSerialization& ) ),
              this, SLOT ( readyRead ( const QQtLongDataSerialization& ) ) );

}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::readyRead ( const QQtLongDataSerialization& l )
{
    static int i = 0;

    //pline() << l;

    const QQtLongDataSerialization& ds0 = l;

    //QQtLongDataSerialization dstemp = ds0;
    //dstemp.dump();

    p3line() << ds0[0].getValue().toString() ;
    p3line() << ds0[1].getValue().toByteArray().size();

    ui->textBrowser->clear();;
    ui->textBrowser->append ( QString::number ( i++ ) + ", "
                              + QString::number ( ds0[1].getValue().toByteArray().size() ) );

    QString cmd_type = ds0[0].getValue().toString() ;
    if ( cmd_type == "json" )
    {
        QByteArray bytes = ds0[1].getValue().toByteArray();
        p3line() << ds0[1];
        //p3line() << qPrintable ( bytes );
        //p3line() << data[0].getValue().toByteArray().size();

        QQtDictionary dict;
        dict.fromJson ( bytes );
        QByteArray bytes1 = dict.toJson ( QJsonDocument::Indented );
        ui->textBrowser->append ( bytes1 );
    }
    else if ( cmd_type == "binary" )
    {
        //QQtLongDataSerialization不支持二进制
        //主要原因在于json结构，toJson，fromJson不支持二进制，仅仅支持字符串。
        QByteArray bytes = ds0[1].getValue().toByteArray();

        bytes = bytes.fromBase64 ( bytes );

        //p3line() << data[1];
        //p3line() << qPrintable ( bytes );
        //p3line() << data[0].getValue().toByteArray().size();

        QFile file ( "aaa.png" );
        if ( file.exists() )
            file.open ( QFile::WriteOnly );
        else
            file.open ( QFile::Truncate | QFile::WriteOnly );
        file.write ( bytes );
        file.close();
    }
    else if ( cmd_type == "empty" )
    {
        QByteArray bytes;
        //QByteArray bytes = ds0[1].getValue().toByteArray();
        p3line() << bytes;
    }
    else if ( cmd_type == "2m" )
    {
        QByteArray bytes = ds0[1].getValue().toByteArray();

        bytes = bytes.fromBase64 ( bytes );
        bytes = qUncompress ( bytes );
        //p3line() << data[1];
        //p3line() << qPrintable ( bytes );
        //p3line() << data[0].getValue().toByteArray().size();

        QFile file ( "image.zip" );
        if ( file.exists() )
            file.open ( QFile::WriteOnly );
        else
            file.open ( QFile::Truncate | QFile::WriteOnly );
        file.write ( bytes );
        file.close();
    }
    else if ( cmd_type == "json_2" )
    {
        QByteArray bytes = ds0[1].getValue().toByteArray();
        //p3line() << data[1];
        //p3line() << qPrintable ( bytes );
        //p3line() << data[0].getValue().toByteArray().size();

        QQtDictionary dict;
        dict.fromJson ( bytes );
        QByteArray bytes1 = dict.toJson ( QJsonDocument::Indented );
        static int size = 0;
        ui->textBrowser->append ( QString::number ( size + bytes1.size() ) );
        size += bytes1.size();
    }

}
